package com.jiajia.study.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @Title: RabbitMessageListener
 * @Author zjj
 * @Package com.jiajia.study.listener
 * @Date 2024/3/9 17:53
 * @description:
 */

@Component
@Slf4j
public class RabbitMessageV5Listener {

    private int count = 0;
    private static Integer POLL_SIZE = 3;

    @RabbitListener(queues = {"dlx.queue.normal.v5"})
    public void receive(Message message, Channel channel){
        count++;
        log.info("count is : {}",count);
        byte[] body = message.getBody();
        String msg = new String(body);
        /**
         * 参数一 : 表示消息的唯一标识
         * 参数二 : true 表示连带之前的消息一起给确认了，false 表示只确认当前消息
         *
         */
        try {
            if(count%POLL_SIZE == 0){
                int i = 10/0;
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            try {
                /**
                 * 参数一 : 表示消息的唯一标识
                 * 参数二 : false 不重新入队列，true 重新入队列,不重新入队列的，如果绑定了死信队列，就进入到死信队列中
                 */
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException ioException) {
                ioException.printStackTrace();
                throw new RuntimeException(e);
            }
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        log.info("receive message is : {}",msg);
    }



}
